package com.pda.gongpin.ui.fullmove;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.pda.gongpin.R;
import com.pda.gongpin.bean.MoveWareAreaBean;
import com.pda.gongpin.bean.SelectBean;
import com.pda.gongpin.bean.StorageBinIdBean;
import com.pda.gongpin.bean.WareAreaBean;
import com.pda.gongpin.bean.request.StorageBinQueryRequest;
import com.pda.gongpin.contract.FullMoveContract;
import com.pda.gongpin.presenter.FullMovePresenter;
import com.pda.gongpin.utils.PopUtisl;
import com.pda.gongpin.view.ScanButView;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = AppActivityKey.FULLMOVEACTIVITY)
public class FullMoveActivity extends BaseMvpActivity<FullMovePresenter> implements FullMoveContract.View {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_fromWareAreaId)
    TextView tvFromWareAreaId;
    @BindView(R.id.tv_fromStorageBinId)
    TextView tvFromStorageBinId;
    @BindView(R.id.tv_toWareAreaId)
    TextView tvToWareAreaId;
    @BindView(R.id.tv_toStorageBinId)
    TextView tvToStorageBinId;
    @BindView(R.id.sb_scan)
    ScanButView sbScan;
    @BindView(R.id.tv_move)
    TextView tvMove;
    List<MoveWareAreaBean> moveWareAreaBeanList=new ArrayList<>();

    @Override
    public FullMovePresenter getPresenter() {
        return new FullMovePresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_full_move;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initLinsenterner() {

    }

    @Override
    public void getData() {
        presenter.highWareAreaDropdownBox();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    SelectBean fromWareArea, toWareArea;

    @OnClick({R.id.iv_back, R.id.tv_fromWareAreaId, R.id.tv_toWareAreaId, R.id.tv_move})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finishActivity();
                break;
            case R.id.tv_fromWareAreaId:
                if (moveWareAreaBeanList != null) {
                    List<SelectBean> selectBeans = new ArrayList<>();
                    for (MoveWareAreaBean moveWareAreaBean : moveWareAreaBeanList) {
                        SelectBean selectBean = new SelectBean(moveWareAreaBean.getWareAreaName(), moveWareAreaBean.getWareAreaId());
                        selectBeans.add(selectBean);
                    }
                    PopUtisl.showSelectPop(this, selectBeans, tvFromWareAreaId, new PopUtisl.SelectClick() {
                        @Override
                        public void onClick(SelectBean bean) {
                            fromWareArea = bean;
                            tvFromWareAreaId.setText(bean.getName());
                        }
                    }, "请选择移出库区");
                }
                break;
            case R.id.tv_toWareAreaId:
                if (moveWareAreaBeanList != null) {
                    List<SelectBean> selectBeans = new ArrayList<>();
                    for (MoveWareAreaBean moveWareAreaBean : moveWareAreaBeanList) {
                        SelectBean selectBean = new SelectBean(moveWareAreaBean.getWareAreaName(), moveWareAreaBean.getWareAreaId());
                        selectBeans.add(selectBean);
                    }
                    PopUtisl.showSelectPop(this, selectBeans, tvToWareAreaId, new PopUtisl.SelectClick() {
                        @Override
                        public void onClick(SelectBean bean) {
                            toWareArea = bean;
                            tvToWareAreaId.setText(bean.getName());
                        }
                    }, "请选择移入库区");
                }
                break;
            case R.id.tv_move:
                if (TextUtils.isEmpty(tvFromWareAreaId.getText())) {
                    showMessage("移出库区不能为空！");
                    return;
                }
                if (TextUtils.isEmpty(tvFromStorageBinId.getText())) {
                    showMessage("移出库位不能为空！");
                    return;
                }
                if (TextUtils.isEmpty(tvToWareAreaId.getText())) {
                    showMessage("移入库区不能为空！");
                    return;
                }
                if (TextUtils.isEmpty(tvToStorageBinId.getText())) {
                    showMessage("移入库位不能为空！");
                    return;
                }
                presenter.wholePalletMove(fromWareArea.getId(), tvFromStorageBinId.getText().toString(), toWareArea.getId(), tvToStorageBinId.getText().toString());
                break;
        }
    }

    @Override
    public void onSuccess() {

    }

    @Override
    public void setWareList(List<MoveWareAreaBean> list) {
        moveWareAreaBeanList.addAll(list);
    }
    String fromStorageBinId,toStorageBinId;
    @Override
    public void setStorageList(List<String> list, boolean isTo) {
        if (isTo){
            List<SelectBean> selectBeans = new ArrayList<>();
            for (String moveWareAreaBean : list) {
                SelectBean selectBean = new SelectBean(moveWareAreaBean, moveWareAreaBean);
                selectBeans.add(selectBean);
            }
            PopUtisl.showSelectPop(this, selectBeans, tvToStorageBinId, new PopUtisl.SelectClick() {
                @Override
                public void onClick(SelectBean bean) {
                    toStorageBinId = bean.getName();
                    tvToStorageBinId.setText(bean.getName());
                }
            }, "请选择移入库位");
        }else {
            List<SelectBean> selectBeans = new ArrayList<>();
            for (String moveWareAreaBean : list) {
                SelectBean selectBean = new SelectBean(moveWareAreaBean, moveWareAreaBean);
                selectBeans.add(selectBean);
            }
            PopUtisl.showSelectPop(this, selectBeans, tvFromWareAreaId, new PopUtisl.SelectClick() {
                @Override
                public void onClick(SelectBean bean) {
                    fromStorageBinId = bean.getName();
                    tvFromStorageBinId.setText(bean.getName());
                }
            }, "请选择移出库位");
        }
    }

    @OnClick({R.id.tv_fromStorageBinId, R.id.tv_toStorageBinId})
    public void onViewClicked1(View view) {
        switch (view.getId()) {
            case R.id.tv_fromStorageBinId:
                if (fromWareArea == null){
                    showMessage("请先选择移出库区");
                    return;
                }
                presenter.highStorageBinDropdownBox(fromWareArea.getId(),false);
                break;
            case R.id.tv_toStorageBinId:
                if (toWareArea == null){
                    showMessage("请先选择移入库区");
                    return;
                }
                presenter.highStorageBinDropdownBox(toWareArea.getId(),true);
                break;
        }
    }
}
